package com.ruoyi.electricity.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.electricity.domain.CoalMineInfo;
import com.ruoyi.electricity.service.ICoalMineInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 煤矿信息配置Controller
 *
 * @author ljw
 * @date 2023-12-06
 */
@RestController
@RequestMapping("/electricity/coalMineInfoConfig")
public class CoalMineInfoController extends BaseController
{
//    @Value("${ProtocolCode:3}")
//    private String ProtocolCode;\

    @Autowired
    private ICoalMineInfoService coalMineInfoService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询煤矿信息配置列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CoalMineInfo coalMineInfo)
    {
        startPage();
        List<CoalMineInfo> list = coalMineInfoService.selectCoalMineInfoList(coalMineInfo);
//        System.out.println("=======================================测试ProtocolCode：" + ProtocolCode);
        return getDataTable(list);
    }

    /**
     * 查询煤矿信息配置列表
     */
    @GetMapping("/list/v2")
    public TableDataInfo list(CoalMineInfo coalMineInfo, HttpServletRequest httpServletRequest)
    {
       Long unitCode = tokenService.getLoginUserUnitCode(httpServletRequest);
       coalMineInfo.setUnitCode(unitCode);
       List<CoalMineInfo> list = coalMineInfoService.selectCoalMineInfoListV2(coalMineInfo);
        return getDataTable(list);
    }

    /**
     * 导出煤矿信息配置列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, CoalMineInfo coalMineInfo)
    {
        List<CoalMineInfo> list = coalMineInfoService.selectCoalMineInfoList(coalMineInfo);
        ExcelUtil<CoalMineInfo> util = new ExcelUtil<CoalMineInfo>(CoalMineInfo.class);
        util.exportExcel(response, list, "煤矿信息配置数据");
    }

    /**
     * 获取煤矿信息配置详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(coalMineInfoService.selectCoalMineInfoById(id));
    }

    /**
     * 新增煤矿信息配置
     */
    @PostMapping
    public AjaxResult add(@RequestBody CoalMineInfo coalMineInfo)
    {
        if (coalMineInfo.getItem() == null) {
            return error("配置项不能为空");
        }
        if (coalMineInfo.getValue() == null) {
            return error("配置值不能为空");
        }

        return toAjax(coalMineInfoService.insertCoalMineInfo(coalMineInfo));
    }

    /**
     * 修改煤矿信息配置
     */
    @PutMapping
    public AjaxResult edit(@RequestBody CoalMineInfo coalMineInfo)
    {
        return toAjax(coalMineInfoService.updateCoalMineInfo(coalMineInfo));
    }

    /**
     * 删除煤矿信息配置
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(coalMineInfoService.deleteCoalMineInfoByIds(ids));
    }
}
